import java.awt.Color;
import java.awt.Graphics;
import java.util.Map;

import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSObject;

public class DPowerIron extends Script implements PaintListener {

	public static final int[] Rock = new int[] { 2093, 2093, 2092, 9717, 9719,
			9718, 11956, 11955, 11954, 11557, 37307, 37309, 10947, 31071,
			31072, 31073, 37308, 14913, 14914 };

	public int startLevel = 0;

	public long startTime = System.currentTimeMillis();

	public int startXP = 0;

	public static int MouseSpeed = 4;
	int xx1 = random(575, 580);
	int yy1 = random(379, 383);

	int xx2 = random(540, 545);

	int yy2 = random(416, 424);

	int xx3 = random(575, 580);
	int yy3 = random(402, 407);
	int xx4 = random(540, 545);
	int yy4 = random(446, 449);
	int xx5 = random(575, 580);
	int yy5 = random(440, 445);
	int xx6 = random(540, 545);
	int yy6 = random(458, 463);

	public void Drop() {
		if (getCurrentTab() == Constants.TAB_INVENTORY) {

			clickMouse(xx1, yy1, 3, 3, false);

			clickMouse(xx2, yy2, 3, 3, true);

			clickMouse(xx3, yy3, 3, 3, false);

			clickMouse(xx4, yy4, 3, 3, true);

			clickMouse(xx5, yy5, 3, 3, false);

			clickMouse(xx6, yy6, 3, 3, true);
		} else {
			openTab(Constants.TAB_INVENTORY);
		}

	}

	@Override
	public String getAuthor() {
		return "Dwu";
	}

	@Override
	public String getName() {
		return "Iron powerminer";
	}

	@Override
	public String getScriptCategory() {
		return "Mining";
	}

	@Override
	public int loop() {
		if (!isInventoryFull()) {
			Mine();
		}
		if (isInventoryFull()) {
			Drop();
		}
		return random(100, 200);
	}

	public void Mine() {
		if (getEnergy() > random(25, 100)) {
			setRun(true);
		}
		if (getMyPlayer().getAnimation() == -1 && !getMyPlayer().isMoving()) {
			final RSObject obj = findObject(DPowerIron.Rock);
			if (obj != null) {
				if (distanceTo(obj.getLocation()) > 5) {
					turnToTile(obj.getLocation());
				}
				if (distanceTo(obj.getLocation()) > 7) {
					log.info("No rocks near, waiting.");
				} else {
					atObject(obj, "Mine");
					wait(random(50, 100));
				}
			} else {
				wait(20);
				log.info("No rocks near, waiting.");
			}
		}
	}

	public void onRepaint(final Graphics g) {
		long millis = System.currentTimeMillis() - startTime;
		final long hours = millis / (1000 * 60 * 60);
		millis -= hours * 1000 * 60 * 60;
		final long minutes = millis / (1000 * 60);
		millis -= minutes * 1000 * 60;
		final long seconds = millis / 1000;
		final int XPChange = skills.getCurrentSkillExp(14) - startXP;
		final int LevelChange = skills.getCurrentSkillLevel(14) - startLevel;
		g.setColor(Color.red);
		g.drawString("Runtime: " + hours + " hours " + minutes + " minutes "
				+ seconds + " seconds.", 200, 380);
		g.drawString("Current: " + skills.getCurrentSkillLevel(14)
				+ " levels and " + skills.getCurrentSkillExp(14) + " exp.",
				200, 395);
		g.drawString("Gained: " + LevelChange + " levels and " + XPChange
				+ " exp.", 200, 410);
		g.drawString(skills.getXPToNextLevel(14) + " XP to next level, we are "
				+ skills.getPercentToNextLevel(14) + "% to next level.", 200,
				425);
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		log.info("Welcome to powermining world, we got cookies.");
		startTime = System.currentTimeMillis();
		if (isLoggedIn()) {
			startLevel = skills.getCurrentSkillLevel(14);
			startXP = skills.getCurrentSkillExp(14);
		}
		return true;
	}
}